import com.adobe.serialization.json.JSON;

import mx.charts.AxisRenderer;
import mx.collections.ArrayCollection;
import mx.collections.Sort;
import mx.collections.SortField;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

private var config:Object;
private var modelConfig:Object;
private var currLevel:String;
private var signalTotalCount:int=0;
private var tmpsignalTotalCount:int;
[Bindable]
public var modelPartList:ArrayCollection;
[Bindable]
private var aTemp:Array= new Array();
private var expall:Number=0;
private var newonex:Number=0;
[Bindable]
private var averageArray:Array=new Array();
private var expaveragevalue:Number=0;
[Bindable]
private var averageArrayfinal:Array=new Array();
[Bindable]
private var exprDatachartfinal:Object=new Object();


private function handle_config_files(event:ResultEvent):void {
	if (event.target.url == "arabidopsis_map.json") {
		config = (JSON.decode(String(event.result)));
		modelConfig= (JSON.decode(String(event.result)));
		loadPolFile(modelConfig.settings.webservice.policy_file);
	}
	//submitAGI();
}
private function onClickbutton():void{
	averageArrayfinal=new Array();
	averageArray=new Array();
	aTemp= new Array();
	signalTotalCount=0;
	expall=0;
	expaveragevalue=0;
	newonex=0;
	submitAGI();
}

private function submitAGI():void{
	
	var base:String = modelConfig.settings.webservice.src + "?primaryGene=" + input_AGI.text;
	for (var objectName:String in modelConfig.modelmap) {
		var samplePos:int = 1
		//if(objectName!="control")	{
		for (samplePos; samplePos < modelConfig.modelmap[objectName].length; samplePos++) {
			serve_getExpressionData.url = base + "&sample=" + modelConfig.modelmap[objectName][samplePos].toLowerCase();
			serve_getExpressionData.send();
			signalTotalCount++;
		}//}
		
	}
}

private function expressionDataResult(event:ResultEvent):Boolean {
	var exprData:Object = JSON.decode(String(event.result.toString()));
	exprDatachartfinal = exprData;
	
	for (var name:String in exprData) {
		var numberValue:Number = new Number(exprData[name]);
		expall+=numberValue;//parseFloat().toPrecision(5);
		trace("parseFloat: "+expall);
		
		aTemp.push({data:exprData[name],label:name});
		
		
	}
	//var tmpname:String="roots_19";
	
	
	if(aTemp.length==signalTotalCount){
		expaveragevalue=expall/signalTotalCount;
	}
	
	if(expaveragevalue!=0){
		newonex=expaveragevalue;
		callForMean();
	}
	
	if(aTemp.length==signalTotalCount  ){
		
		modelPartList=new ArrayCollection(aTemp);
		sortCollection(modelPartList);
	}
	return true;
}



private function callForMean():void{
	for(var z:int=0;z<aTemp.length;z++){
		averageArrayfinal.push({afName:aTemp[z].label,afData:newonex});
	}
}


////////////////////////////////////////////////////////////////////////////////////
private function sum(arr:Array):Number {
	var result:Number = 0;
	for (var i:int = 0; i < arr.length; i++) {
		result += arr[i];
	}
	return result;
}
private function average(arr:Array):Number {
	return (sum(arr) / arr.length);
}


private function sortCollection(arrayCollection : ArrayCollection) : void
{
	//Create the sort field
	var dataSortField:SortField = new SortField();
	
	//name of the field of the object on which you wish to sort the Collection
	dataSortField.name = "label";
	dataSortField.caseInsensitive = true;
	
	/*
	If you wish to perform numeric sort then set:
	dataSortField.numeric = true;
	*/
	
	//create the sort object
	var dataSort:Sort = new Sort();
	dataSort.fields = [dataSortField];
	
	arrayCollection.sort = dataSort;
	//refresh the collection to sort
	arrayCollection.refresh();
	
	
}


/**
 * Handle failed HTTPService component requests.
 */
private function expressionDataResultFault(event:FaultEvent):Boolean {
	//Alert.show(event.fault.content.toString());
	
	//changeStatus("There was an error loading webservice data, please try again.");
	return true;
}

/**
 * Retrieves the crossdomain file for the web-service policy file.
 */
private function loadPolFile(url:String):void {
	
	Security.loadPolicyFile(url);
}